from django.db import models
from django.utils.timezone import now


# Create your models here.

class UserPassword(models.Model):
    """用户密码表"""
    password = models.CharField(verbose_name='用户密码', max_length=1000, blank=False, default=None)

    class Meta:
        verbose_name = '用户密码'
        verbose_name_plural = verbose_name
        db_table = 'account_UserPassword'


class UserInfo(models.Model):
    """用户账户表"""
    roles = {
        # ('515400', '总管理员'),
        ('99', '认证领队'),
        ('4', '普通用户(已验证邮箱)'),
        ('5', '普通用户(未验证邮箱)'),
        ('6', '黑名单用户'),
    }

    sex_choice = {
        ('male', '男'),
        ('female', '女'),
        ('secrecy', '保密')
    }
    '''基础信息'''

    email = models.EmailField(verbose_name='邮箱', blank=False, default=None, unique=True)

    nickname = models.CharField(verbose_name='用户昵称', max_length=20, default=None, blank=False, unique=True)

    password = models.ForeignKey(UserPassword, verbose_name='密码', default='', blank=False, on_delete=models.CASCADE)

    """详细信息"""

    signature = models.CharField(verbose_name='个性签名', max_length=100, default='这个人很懒，什么都没写...', blank=True)

    user_role = models.CharField(verbose_name='用户身份', max_length=6, choices=roles, default=5)

    user_sex = models.CharField(verbose_name='用户性别', max_length=10, choices=sex_choice, default='secrecy')

    realName = models.CharField(verbose_name='真实姓名', max_length=10, default='', blank=True)

    phone_number = models.CharField(verbose_name='手机号码', default='', blank=True, max_length=100)

    head_portrait = models.ImageField(verbose_name='头像', max_length=2000, blank=True, upload_to='head_portrait')

    join_date = models.DateField(verbose_name='注册时间', default=now, blank=False)

    class Meta:
        verbose_name = '用户账户'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'account_UserInfo'
        ordering = ['-join_date']

    def __str__(self):
        return self.email


class UserLog(models.Model):
    """用户记录表"""
    related_user = models.ForeignKey(UserInfo, verbose_name='关联用户', default='', blank=False, on_delete=models.CASCADE)

    last_login_time = models.DateTimeField(verbose_name='最后登录时间', default=now, blank=False)

    Number_of_activities_attended = models.IntegerField(verbose_name='参加活动的次数', blank=False, default=0)

    Number_of_activities_initiated = models.IntegerField(verbose_name='发起活动的次数', blank=False, default=0)

    Number_of_ongoing_activities = models.IntegerField(verbose_name='进行中的活动数量', blank=False, default=0)

    class Meta:
        verbose_name = '用户记录信息'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'account_UserLog'

    def __str__(self):
        return self.related_user.email


class UserSetting(models.Model):
    """用户设置"""
    related_user = models.ForeignKey(UserInfo, verbose_name='关联用户', default='', blank=False, on_delete=models.CASCADE)
    # 隐私设置
    privacy_choice = {
        ('1', '公开'),
        ('2', '关闭'),
        ('3', '只允许好友查看')
    }
    activity_privacy = models.CharField(verbose_name='户外活动隐私', max_length=10, default='1', blank=False)

    class Meta:
        verbose_name = '用户设置'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'account_UserSetting'

    def __str__(self):
        return self.related_user.email


class UserCollectionActivity(models.Model):
    """用户活动收藏"""
    from app.outdoor.models import Activity
    activity = models.ManyToManyField(Activity, verbose_name='关联活动', default='', blank=True)

    related_user = models.ForeignKey(UserInfo, verbose_name='关联用户', default='', blank=False, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '用户活动收藏表'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'account_UserCollectionActivity'

    def __str__(self):
        return str(self.related_user.email)


class EmailVerifyRecord(models.Model):
    """邮箱验证码"""
    type = {
        ('active', '激活'),
        ('forget', '找回密码')
    }
    status = {
        ('used', '已使用'),
        ('not_use', '未使用')
    }
    code = models.CharField(verbose_name='验证码', unique=True, max_length=20)
    email = models.EmailField(verbose_name='邮箱', max_length=50)

    send_type = models.CharField(verbose_name='验证类型', choices=type, max_length=10)
    code_status = models.CharField(verbose_name='验证码使用情况', max_length=10, choices=status, default='not_use')

    class Meta:
        verbose_name = '邮箱验证码'
        verbose_name_plural = verbose_name
        db_table = 'account_EmailVerifyCode'

    def __str__(self):
        return self.email


class UserLeaderVerifyForm(models.Model):
    """领队认证表单"""
    status_choice = {
        ('1', '等待审核'),
        ('2', '已通过'),
        ('3', '已驳回')
    }

    related_user = models.ForeignKey(UserInfo, verbose_name='关联用户', default='', blank=False, on_delete=models.CASCADE)

    description = models.TextField(verbose_name='描述资料', default='', blank=True)

    supporting_file = models.FileField(verbose_name='辅助文件材料', upload_to='verify_file', blank=True)

    status = models.CharField(verbose_name='表单状态', max_length=20, default='1', blank=False, choices=status_choice)

    create_time = models.DateTimeField(verbose_name='提交时间', default=now)

    reject_information = models.TextField(verbose_name='驳回信息', default='', blank=True)

    class Meta:
        verbose_name = '领队认证表单'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'account_UserLeaderVerifyForm'
        ordering = ['-create_time']
